package com.springmvc.utils.mj;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.springmvc.cache.CacheKey;
import com.springmvc.cache.MapCacheManager;
import com.springmvc.utils.JsonUtils;

public class RoomUtil {

	//	@Autowired
	//	private static IUserService	   userService = (IUserService) SpringUtils.getBean(IUserService.class);
	//
	//	@Autowired
	//	private static IRoomService	   roomService = (IRoomService) SpringUtils.getBean(IRoomService.class);

	private static MapCacheManager cache = MapCacheManager.getInstance();

	public static void initRoom(String roomid) {

		MJRoom mjRoom = new MJRoom();
		mjRoom.setPondmj(Majiang.getMJDeque());//初始化麻将池
		mjRoom.setPlaymj("1w");//当前出的牌
		mjRoom.setPlayuserno("u1");//出牌人
		mjRoom.setNextplayuserno("u2");//出牌人下一个人的id
		mjRoom.setPlaytype("1");// 1胡 2杠对 3吃（前端判断计时状态）
		initreadyuserno(mjRoom, 4);// 准备用户 key 1 2 3 4 value 用户id

		initmj(mjRoom, "u1", 10);
		initmj(mjRoom, "u2", 0);
		initmj(mjRoom, "u3", 0);
		initmj(mjRoom, "u4", 0);

		initmmj(mjRoom, "u1", 0);
		initmmj(mjRoom, "u2", 0);
		initmmj(mjRoom, "u3", 0);
		initmmj(mjRoom, "u4", 0);
		List<String> centermj = mjRoom.getCentermj();
		if (centermj == null) {
			centermj = new ArrayList<String>();
		}
		for (int i = 0; i < 50; i++) {
			centermj.add(mjRoom.nextMj());
		}
		mjRoom.setCentermj(centermj);

		cache.set(CacheKey.MJRoom(roomid), mjRoom);//房间名称

		System.out.println(JsonUtils.obj2JsonString(cache.get(CacheKey.MJRoom(roomid))));
	}

	private static void initmj(MJRoom mjRoom, String key, int n) {
		Map<String, List<String>> usermj = mjRoom.getUsermj();
		//		for (int i = 0; i < n; i++) {
		//			List<String> list = usermj.get(key);
		//			if (list == null) {
		//				list = new ArrayList<String>();
		//			}
		//			list.add(mjRoom.nextMj());
		//			usermj.put(key, list);
		//		}

		List<String> list = usermj.get(key);
		if (list == null) {
			list = new ArrayList<String>();
		}
		if ("u1".equals(key)) {
			list.add("3万");
			list.add("4万");
			list.add("5万");
			list.add("5万");
			list.add("5万");
			list.add("5万");
			list.add("6万");
			list.add("7万");
			list.add("0东");
			list.add("0东");
			list.add("0东");
			list.add("0东");
			list.add("1中");
			list.add("1中");
			list.add("1中");
			list.add("2發");
			list.add("3口");
		}
		usermj.put(key, list);
	}

	private static void initmmj(MJRoom mjRoom, String key, int n) {
		Map<String, List<String>> usermj = mjRoom.getUsermmj();
		List<String> list = usermj.get(key);
		if (list == null) {
			list = new ArrayList<String>();
		}
		for (int i = 0; i < n; i++) {
			list.add(mjRoom.nextMj());
		}
		usermj.put(key, list);
	}

	private static void initreadyuserno(MJRoom mjRoom, int n) {
		Map<String, String> readyuserno = mjRoom.getReadyuserno();
		for (int i = 1; i <= n; i++) {
			readyuserno.put("u" + i, i + "");
		}
	}

	public static void main(String[] args) {
		initRoom("1");
	}
}
